Overview of Forms in JavaScript

JavaScript allows you to create, control and query HTML forms and their elements. The document object stores a property called forms. This property contains an array of objects corresponding to each of the forms in an HTML document. These forms (each marked with a <FORM>tag are stored in source-code order and can be accessed by index. For example, the first form in a document is stored as document.forms[0], the second as document.forms[1], and so forth. The JavaScript Cookbook Inspector allows you to interactively explore a document's forms and elements.

Top-level form methods and properties
action Specifies the destination URL for form submission, e.g., myForm.action.
elements Accesses an array of objects corresponding to the elements of a form such as buttons, text inputs, and radio buttons, e.g., myForm.elements.
encoding Accesses the MIME encoding of the form, e.g., myForm.encoding.
length The number of elements in a form, e.g., myForm.length.
method The method used for sending form input to a server. Typically either "get" or "post", e.g., myForm.method.
name The name of the form, e.g., myForm.name.
submit() This method submits a form, e.g., myForm.submit().
target The target of a form, e.g., myForm.target.
Element-specific form methods and properties
blur() Removes focus from a password, select, text or textarea object, e.g., myElement.blur().
checked Specifies the selection state of a checkbox or radio button as a boolean value, e.g., myCheckbox.checked or myRadioSet[index].checked.
click() Simulates a mousepress on a button, radio button, or checkbox, e.g., myElement.click().
defaultChecked Specifies the default selection state of a checkbox or radio button regardless of the current value, e.g., myCheckbox.defaultChecked or myRadioSet[index].defaultChecked.
defaultSelected Specifies the default selection of an option in a select object regardless of the current value, e.g., mySelect.options[i].defaultSelected = false.
defaultValue Specifies the default text for a password, text or textarea object regardless of the current value, e.g., myText.defaultValue.
focus() Sets focus on a password, select, text or textarea object, e.g., myElement.focus().
index Specifies an integer representing the index of a select object's current selection, counting from zero, e.g., mySelect.options[anInteger].index.
name Returns the name of a form element, e.g., myElement.name.
options Returns the options array for a select object, e.g., mySelect.options.
select() Selects the text within the input area of a password, text or textarea object, e.g., myElement.select().
selected The current selection state of an option in a select object, e.g., mySelect.options[i].selected.
selectedIndex The index (counting from zero) of the current selection for a selection object, e.g., mySelect.options.selectedIndex.
text The text which follows an option tag in a select object. You may change this value but the screen will not update, e.g., mySelect.options[i].text.
value The text in a hidden, text, or textarea object, or the hidden string in a password object (which, for security, cannot be read by a JavaScript function if set by a user), or the string displayed on a button, reset, or submit object, or the on/off state of a checkbox or radio object, or the contents of a special "VALUE" attribute of an options array, e.g., myElement.value.
Form Event Handlers
onBlur Called when a select, text, or textarea field loses focus.
onChange Called when a modified (changed) select, text or textarea field loses focus.
onClick Called when a button, checkbox, radio, link, reset, or submit is clicked.
onFocus Called when a select, text or textarea field gains focus.
onSelect Called when user selects text from a text or textarea object.
onSubmit Called when a user submits a form. Make your onSubmit handler return either true (submit the form) or false (do not submit the form), e.g., onSubmit="return(confirm('really?'))".
Copyright ©1998 by Charles River Media, All Rights Reserved